﻿using Static;
using Model.Enums;
using Model.RunTimeModel;
using System.Collections.Generic;
using gs_winpos_group_start.Model;
using Model.ApiMode.Balance;
using Model.ProductModel;

namespace CoreShop.Model.OrderModel
{
    /// <summary>
    /// 新增订单接口
    /// 订单信息
    /// </summary>
    public class PosOrderHeadDto
    {
        /// <summary>
        /// 收银订单主id
        /// </summary>
        public string posOrderHeadId { get; set; }

        /// <summary>
        /// 商户号
        /// </summary>
        public string tenantId { get; set; }

        /// <summary>
        /// 组织
        /// </summary>
        public string organizationId { get; set; }

        /// <summary>
        /// 单据号
        /// </summary>
        public string billNo { get; set; }

        /// <summary>
        /// 门店
        /// </summary>
        public string branchId { get; set; }

        /// <summary>
        /// 门店编码
        /// </summary>
        public string branchNo { get; set; }

        /// <summary>
        /// 门店名称
        /// </summary>
        public string branchName { get; set; }

        /// <summary>
        /// 订单金额(原价)
        /// </summary>
        public decimal amountOrder { get; set; }

        /// <summary>
        /// 应收金额
        /// </summary>
        public decimal amountDiscount { get; set; }

        /// <summary>
        /// 实收金额(抹零前）
        /// </summary>
        public decimal amountBalance { get; set; }

        /// <summary>
        /// 优惠总额（订单金额-实收金额)
        /// </summary>
        public decimal amountFact { get; set; }

        /// <summary>
        /// 抹零总额
        /// </summary>
        public decimal amountErase { get; set; }

        /// <summary>
        /// 结算日期
        /// </summary>
        /// <param name=""></param>
        public string billDate { get; set; }

        /// <summary>
        /// 结算时间(日期+时间)
        /// </summary>
        public string billTime { get; set; }

        /// <summary>
        /// 订单类型：1 销售订单 2 按单退货 3 无单退货 4 门店交款单 5 充值订单 6 充值退单 7 会员退款订单  8 好享会员优惠转充值 9 积分充值 10 好享购卡
        /// </summary>
        public string orderBillTypeId { get; set; } = "1";

        /// <summary>
        /// 得到订单类型名称
        /// </summary>
        /// <returns></returns>
        public string GetOrderBillTypeIdName()
        {
            switch(orderBillTypeId)
            {
                case "1":
                    return "销售订单";
                case "2":
                    return "按单退货";
                case "3":
                    return "无单退货";
                case "4":
                    return "门店交款单";
                case "5":
                    return "充值订单";
                case "6":
                    return "充值退单";
                case "7":
                    return "会员退款订单";
                case "8":
                    return "好享会员优惠转充值";
                case "9":
                    return "积分充值";
                default:
                    return "无";
            }
        }

        /// <summary>
        /// 子订单类型 正常订单 2 拼团订单3预售订单4秒杀订单5预订单 6 小程序充值 7 小程序充值退单
        /// </summary>
        public string orderTypeId { get; set; } = "1";

        /// <summary>
        /// 销售类型 1 线上 2 线下
        /// </summary>
        public string saleTypeId { get; set; } = "2";

        /// <summary>
        /// 销售渠道 1 门店 2 微信小程序 3 美团 4 额了么 5 京东 6 其他 7 手工 8 支付宝小程序 9 抖音 10 淘鲜达 11 美团券 12 支付宝团购券
        /// </summary>
        public string saleSortId { get; set; }

        /// <summary>
        /// 销售渠道名称
        /// </summary>
        /// <returns></returns>
        public string GetSaleSortName()
        {
            if (saleSortId == SaleSort.门店.GetHashCode().ToString())
            {
                return SaleSort.门店.ToString();
            }

            if (saleSortId == SaleSort.微信小程序.GetHashCode().ToString())
            {
                return SaleSort.微信小程序.ToString();
            }

            if (saleSortId == SaleSort.美团.GetHashCode().ToString())
            {
                return SaleSort.美团.ToString();
            }

            if (saleSortId == SaleSort.饿了么.GetHashCode().ToString())
            {
                return SaleSort.饿了么.ToString();
            }

            if (saleSortId == SaleSort.京东.GetHashCode().ToString())
            {
                return SaleSort.京东.ToString();
            }

            if (saleSortId == SaleSort.其他.GetHashCode().ToString())
            {
                return SaleSort.其他.ToString();
            }

            if (saleSortId == SaleSort.手工.GetHashCode().ToString())
            {
                return SaleSort.手工.ToString();
            }

            if (saleSortId == SaleSort.支付宝小程序.GetHashCode().ToString())
            {
                return SaleSort.支付宝小程序.ToString();
            }

            if (saleSortId == SaleSort.抖音.GetHashCode().ToString())
            {
                return SaleSort.抖音.ToString();
            }

            if (saleSortId == SaleSort.淘鲜达.GetHashCode().ToString())
            {
                return SaleSort.淘鲜达.ToString();
            }

            if (saleSortId == SaleSort.美团券.GetHashCode().ToString())
            {
                return SaleSort.美团券.ToString();
            }

            if (saleSortId == SaleSort.支付宝团购券.GetHashCode().ToString())
            {
                return SaleSort.支付宝团购券.ToString();
            }

            return "未知";
        }

        /// <summary>
        /// 会员id
        /// </summary>
        public string vipInfoId { get; set; } = "0";

        /// <summary>
        /// 会员号
        /// </summary>
        public string vipNo { get; set; } = "0";

        /// <summary>
        /// 会员Name
        /// </summary>
        public string vipName { get; set; }

        /// <summary>
        /// 原会员积分
        /// </summary>
        public decimal oldPoint { get; set; }

        /// <summary>
        /// 新会员积分
        /// </summary>
        public decimal newPoint { get; set; }

        /// <summary>
        /// 原会员余额
        /// </summary>
        public decimal oldAmt { get; set; }

        /// <summary>
        /// 新会员余额
        /// </summary>
        public decimal newAmt { get; set; }

        /// <summary>
        /// 外部订单号
        /// </summary>
        public string outno { get; set; }

        /// <summary>
        /// 来源订单号 退单时保存关联原外部订单号
        /// </summary>
        public string sourceNo { get; set; }

        /// <summary>
        /// 来源单据id
        /// </summary>
        public string sHeadId { get; set; }

        /// <summary>
        /// 订单状态 1 未生效 5 已生效
        /// </summary>
        public string status { get; set; } = "5";

        /// <summary>
        /// 收银员
        /// </summary>
        public string auditer { get; set; }

        /// <summary>
        /// 配送方式 1 自提 2 外卖 3 快递
        /// </summary>
        public string pickType { get; set; } = "1";

        /// <summary>
        /// 三方平台原始订单号
        /// </summary>
        public string sBillno { get; set; }

        /// <summary>
        /// 门店收银订单从DTO
        /// </summary>
        public List<PosOrderLineDto> billLine { get; set; }

        /// <summary>
        /// 门店收银结算DTO
        /// </summary>
        public List<PosOrderBalDto> billBal { get; set; }

        /// <summary>
        /// 销售优惠信息
        /// </summary>
        public List<PosOrderBillPromoDto> billPromo { get; set; }

        /// <summary>
        /// 券信息
        /// </summary>
        public List<PosOrderBillCouponDto> billCoupon { get; set; }

        /// <summary>
        /// 会员充值单档位赠送记录
        /// </summary>
        public List<PosOrderBillVipPromoDto> billVipPromo { get; set; }

        /// <summary>
        /// -1:结算中
        /// 0:未推送
        /// 1:推送中
        /// 2:已经推送
        /// 3:推送失败
        /// </summary>
        public string sendType { get; set; } = OrderSendType.结算中.GetHashCode().ToString();

        /// <summary>
        /// -1:结算中
        /// 0:未推送
        /// 1:推送中
        /// 2:已经推送
        /// 3:推送失败
        /// </summary>
        public string sendType2 { get; set; } = OrderSendType.结算中.GetHashCode().ToString();

        /// <summary>
        /// -1:结算中
        /// 0:未推送
        /// 1:推送中
        /// 2:已经推送
        /// 3:推送失败
        /// </summary>
        public string sendType3 { get; set; } = OrderSendType.结算中.GetHashCode().ToString();

        /// <summary>
        /// 订单状态名称
        /// </summsary>
        /// <returns></returns>
        public string GetSendTypeName()
        {
            switch (sendType)
            {
                case "-1":
                    return OrderSendType.结算中.ToString();
                case "0":
                    return OrderSendType.未推送.ToString();
                case "1":
                    return OrderSendType.推送中.ToString();
                case "2":
                    return OrderSendType.已推送.ToString();
                case "3":
                    return OrderSendType.推送失败.ToString();
                default:
                    return "未知";
            }

        }

        /// <summary>
        /// 推送次数
        /// </summary>
        public int sendCount { get; set; } = 0;

        /// <summary>
        /// 表id
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 仓库id
        /// </summary>
        public string warehouseId { get; set; }

        /// <summary>
        /// 交班批次id
        /// </summary>
        public string dutycode { get; set; }

        /// <summary>
        /// 是否重印
        /// </summary>
        public bool IsRepeatPrint { get; set; } = false;

        ///// <summary>
        ///// 订单积分
        ///// </summary>
        //public int pointsOrder { get; set; } = 0;
        ///// <summary>
        ///// 0未计算，1已计算
        ///// </summary>
        //public int pointsType { get; set; } = 0;
        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }
    }

    /// <summary>
    /// 门店收银订单从DTO
    /// 商品信息
    /// </summary>
    public class PosOrderLineDto
    {
        /// <summary>
        /// 表id
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 收银订单从id
        /// </summary>
        public string posOrderLineId { get; set; }

        /// <summary>
        /// 收银订单主id
        /// </summary>
        public string posOrderHeadId { get; set; }

        /// <summary>
        /// 仓库
        /// </summary>
        public string warehouseId { get; set; }

        /// <summary>
        /// 商品
        /// </summary>
        public string spuId { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public decimal qtyOrder { get; set; }

        /// <summary>
        /// 已退数量
        /// </summary>
        public decimal qtyBacked { get; set; }

        /// <summary>
        /// 原价
        /// </summary>
        public decimal priceOrder { get; set; }

        /// <summary>
        /// 折后单价
        /// </summary>
        public decimal priceDiscount { get; set; }

        /// <summary>
        /// 结算价
        /// </summary>
        public decimal priceBalance { get; set; }

        /// <summary>
        /// 成本单价
        /// </summary>
        public decimal priceCost { get { return 0; } set { value = 0; } }

        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal amountOrder { get; set; }

        /// <summary>
        /// 折后金额
        /// </summary>
        public decimal amountDiscount { get; set; }

        /// <summary>
        /// 结算金额
        /// </summary>
        public decimal amountBalance { get; set; }

        /// <summary>
        /// 充值赠送金额
        /// </summary>
        public decimal amountZ { get; set; }

        /// <summary>
        /// 成本金额
        /// </summary>
        public decimal amountCost { get { return 0; } set { value = 0; } }

        /// <summary>
        /// 已退订单金额
        /// </summary>
        public decimal amountOrderBack { get; set; }

        /// <summary>
        /// 已退折后金额
        /// </summary>
        public decimal amountDiscountBack { get; set; }

        /// <summary>
        /// 已退结算金额
        /// </summary>
        public decimal amountBalanceBack { get; set; }

        /// <summary>
        /// 已退充值赠送金额
        /// </summary>
        public decimal amountZBack { get; set; }

        /// <summary>
        /// 行状态   1 正常 2 赠送 3 退货 4 促销
        /// </summary>
        public string status { get; set; }

        /// <summary>
        /// 来源主
        /// </summary>
        public string sourceHeadId { get; set; }

        /// <summary>
        /// 来源从
        /// </summary>
        public string sourceLineId { get; set; }


        public string spuCode { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string spuName { get; set; }

        /// <summary>
        /// 规格编码
        /// </summary>
        public string pdtSpuSpecId { get; set; }

        /// <summary>
        /// 表id
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 多条相同数据时作为标识
        /// </summary>
        public string thisCode { get; set; }

        /// <summary>
        /// 退货数量 虚拟字段用于退货时绑定
        /// </summary>
        public decimal TextRetrunQtyNumber { get; set; }

        /// <summary>
        /// 规格
        /// </summary>
        public string specUomName { get; set; }

        /// <summary>
        /// 查询订单详细使用 虚拟字段
        /// </summary>
        public string pdtSpuSpecName { get; set; }

        /// <summary>
        /// 查询订单详细使用 虚拟字段
        /// </summary>
        public string pdtSpuSpecCode { get; set; }

        public string disItem { get; set; }
    }

    /// <summary>
    /// 门店收银结算DTO
    /// 收银信息
    /// </summary>
    public class PosOrderBalDto
    { 
        /// <summary>
        /// 表id
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 收银结算单id
        /// </summary>
        public string posOrderBalId { get; set; }

        /// <summary>
        /// 收银订单主id
        /// </summary>
        public string posOrderHeadId { get; set; }

        /// <summary>
        /// 支付方式id
        /// </summary>
        public string baseBalanceTypeId { get; set; } = string.Empty;

        /// <summary>
        /// 支付方式名称
        /// </summary>
        /// <returns></returns>
        public string GetbaseBalanceTypeName()
        {
            if (InitDataModel.StaticData.baseTenantBalance != null && InitDataModel.StaticData.baseTenantBalance.data != null)
            {
                return InitDataModel.StaticData.baseTenantBalance.data.Find(t =>
                {
                    return t.tenantBalanceTypeId == baseBalanceTypeId
                    || t.balanceTypeCode == baseBalanceTypeId;
                })?.balanceTypeName ?? "未知";
            }
            else
            {
                return "离线";
            }
        }

        /// <summary>
        /// 应收金额
        /// </summary>
        public decimal amountPayment { get; set; }

        /// <summary>
        /// 实收金额 本金
        /// </summary>
        public decimal amountDiscount { get; set; }

        /// <summary>
        /// 客户实付金额(客户实际付款金额) 赠金
        /// </summary>
        public decimal amountFact { get; set; }

        /// <summary>
        /// 可退 应收金额
        /// </summary>
        public decimal amountPaymentBack { get; set; }

        /// <summary>
        /// 可退 实收金额 本金
        /// </summary>
        public decimal amountDiscountBack { get; set; }

        /// <summary>
        /// 可退 客户实付金额(客户实际付款金额) 赠金
        /// </summary>
        public decimal amountFactBack { get; set; }

        /// <summary>
        /// 账户代码(支付对应 OUT_SN 我们生成的支付订单号)
        /// </summary>
        public string accountCode { get; set; } = string.Empty;

        /// <summary>
        /// 支付流水号(线上取支付宝、威信生成的交易单号，成功收款才有值，线下收款由系统自动生成)
        /// </summary>
        public string paymentSn { get; set; } = string.Empty;

        /// <summary>
        /// 第三方支付平台生成的单号(费瑞)
        /// </summary>
        public string orderSn { get; set; } = string.Empty;

        /// <summary>
        /// 余额支付的本金
        /// </summary>
        public decimal amountS { get; set; }

        /// <summary>
        /// 商家优惠
        /// </summary>
        public decimal amountDisshop { get; set; }

        /// <summary>
        /// 平台优惠
        /// </summary>
        public decimal amountDisplat { get; set; }

        /// <summary>
        /// 运费
        /// </summary>
        public decimal freightCharge { get; set; }

        /// <summary>
        /// 运费类型 1 顾客承担  2 公司承担
        /// </summary>
        public string freightType { get; set; } = "2";

        /// <summary>
        /// 配送分类1.未配送  2  已配送
        /// </summary>
        public string freightSort { get; set; } = "2";

        /// <summary>
        /// 手续费
        /// </summary>
        public decimal amountFee { get; set; }

        /// <summary>
        /// 表id
        /// </summary>
        public string orderId { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        public string status { get; set; } = PayStatus.未支付.GetHashCode().ToString();

        /// <summary>
        /// 状态
        /// </summary>
        public string GetStatusName()
        {
            switch (status)
            {
                case "1":
                    return PayStatus.未支付.ToString();
                case "2":
                    return PayStatus.成功.ToString();
                case "3":
                    return PayStatus.支付中.ToString();
                case "4":
                    return PayStatus.交易关闭.ToString();
                case "5":
                    return PayStatus.交易撤销.ToString();
                case "6":
                    return PayStatus.部分退款.ToString();
                case "7":
                    return PayStatus.失败.ToString();
                default:
                    return "未知";
            }
        }

        /// <summary>
        /// 在线支付码
        /// </summary>
        public string authcode { get; set; }

        /// <summary>
        /// 在线支付码类型
        /// </summary>
        public string authcodeType { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string sourceBalId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string sourceHeadId { get; set; }

        /// <summary>
        /// 支付会员信息
        /// </summary>
        //public string vipPaymentDto { get; set; }
    }

    /// <summary>
    /// 销售优惠信息
    /// </summary>
    public class PosOrderBillPromoDto
    {
        /// <summary>
        /// Id
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string remark {  get; set; }
        /// <summary>
        /// 收银优惠从id
        /// </summary>
        public string posOrderPromoId {  get; set; }
        /// <summary>
        /// 收银订单主id
        /// </summary>
        public string posOrderHeadId {  get; set; }
        /// <summary>
        /// 商户id
        /// </summary>
        public string tenantId {  get; set; }
        /// <summary>
        /// 组织id
        /// </summary>
        public string organizationId {  get; set; }
        /// <summary>
        /// 促销编码
        /// </summary>
        public string promoCode { get; set; } = "0";
        /// <summary>
        /// 商品
        /// </summary>
        public string spuId {  get; set; }
        /// <summary>
        /// 商品编码
        /// </summary>
        public string spuCode {  get; set; }
        /// <summary>
        /// 商品名称
        /// </summary>
        public string spuName {  get; set; }

        /// <summary>
        /// 商品编码
        /// </summary>
        public string pdtSpuSpecId { get; set; }
        /// <summary>
        /// 改价前单价
        /// </summary>
        public string beforePrice {  get; set; }
        /// <summary>
        /// 改价后单价
        /// </summary>
        public string afterPrice {  get; set; }
        /// <summary>
        /// 重量
        /// </summary>
        public string qtyOrder {  get; set; }
        /// <summary>
        /// 改价前金额
        /// </summary>
        public string amountBefore {  get; set; }
        /// <summary>
        /// 改价后金额
        /// </summary>
        public string amountAfter {  get; set; }
        /// <summary>
        /// 单据时间
        /// </summary>
        public string billTime {  get; set; }
        /// <summary>
        /// 单据号
        /// </summary>
        public string billNo {  get; set; }
        /// <summary>
        /// 门店
        /// </summary>
        public string branchId {  get; set; }

        /// <summary>
        /// 表id
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 优惠类型 1 会员优惠2 活动优惠3 单品折扣 4 整单折扣 5 抹零 6 劵优惠
        /// </summary>
        public string promoType { get; set; }
    }

    /// <summary>
    /// 优惠券信息
    /// </summary>
    public class PosOrderBillCouponDto
    {
        /// <summary>
        /// id
        /// </summary>
        public long Id { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }
        /// <summary>
        /// 从id
        /// </summary>
        public string posOrderCouponId { get; set; }
        /// <summary>
        /// 主id
        /// </summary>
        public string posOrderHeadId { get; set; }
        /// <summary>
        /// 券号
        /// </summary>
        public string couponInfoId { get; set; }
        /// <summary>
        /// 券码
        /// </summary>
        public string couponNo { get; set; }
        /// <summary>
        /// 券名称
        /// </summary>
        public string couponName { get; set; }
        /// <summary>
        /// 券面额
        /// </summary>
        public string amount { get; set; }
        /// <summary>
        /// 券实收金额
        /// </summary>
        public string amountFact { get; set; }
        /// <summary>
        /// 券抵扣金额
        /// </summary>
        public string amountVerify { get; set; }

        /// <summary>
        /// 表id
        /// </summary>
        public string orderId { get; set; }
    }
    
    /// <summary>
    /// 会员充值单档位赠送记录表
    /// </summary>
    public class PosOrderBillVipPromoDto 
    {
        /// <summary>
        /// 充值订单活动从id
        /// </summary>
        public long posOrderVipPromoId { get; set; }

        /// <summary>
        /// 收银订单主id
        /// </summary>
        public string posOrderHeadId { get; set; }

        /// <summary>
        /// 收银订单主id 使用这个
        /// </summary>
        public string posTxtOrderHeadId { get; set; }

        /// <summary>
        /// 活动类型 1 积分 2 券
        /// </summary>
        public int promoType { get; set; }

        /// <summary>
        /// 活动id
        /// </summary>
        public long promoId { get; set; }

        /// <summary>
        /// 活动名称
        /// </summary>
        public string promoName { get; set; }

        /// <summary>
        /// 活动值
        /// </summary>
        public string value { get; set; }

        /// <summary>
        /// 券id
        /// </summary>
        public long couponId { get; set; }
    }

    /// <summary>
    /// 用于本表标识
    /// </summary>
    public class GsTableId
    {
        /// <summary>
        /// 本表id
        /// </summary>
        public string tableId;
    }

    /// <summary>
    /// 订单查询
    /// </summary>
    public class SearchOrderDataModel : ApiReturnModel
    {
        public new List<PosOrderHeadDto> data { get; set; }
    }
}
